package fr.mywiki.view.web.struts.action.entry;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import fr.mywiki.view.web.struts.form.entry.EntryListForm;

public class EntryListAction extends DispatchAction {
	/**
	 * When initializing the formular EntryListForm, the list of entry ids is passed through the
	 * request via an attribute whose name id list_entries_ids.
	 */
	public static final String LIST_ENTRIES_IDS = "list_entries_ids";
	
	/** When searching an entry, this request parameter contains the search string. */
	public static final String SEARCH_STRING = "search_string";
	
	/**
	 * Displays a list of entries. A list of <code>EntryLite</code> objects is passed through
	 * the request.
	 */
	public ActionForward displayList(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) throws RemoteException {
		EntryListForm form = (EntryListForm) actionForm;
		form.init((Collection) request.getAttribute(LIST_ENTRIES_IDS));
		form.setSearchString(request.getAttribute(SEARCH_STRING).toString());
		
		return mapping.findForward("display");
	}

	/**
	 * This method is used when the user pushes on a link of the formular (which corresponds to a
	 * specific entry.
	 */
	public ActionForward displayEntry(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		request.setAttribute(EntryPageAction.ENTRY_ID,
				((EntryListForm) actionForm).getEntryId());
		return mapping.findForward("displayEntry");
	}
	
	/**
	 * Forwards to the creation formular with the title being the search string
	 */
	public ActionForward createEntry(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		EntryListForm form = (EntryListForm) actionForm;
		request.setAttribute(EditEntryAction.ENTRY_TITLE,
				form.getSearchString());
		return mapping.findForward("create");
	}
}
